<?php
namespace controllers\admin;

use libs\request as request;
use libs\registry as Registry;

class tresci extends \controllers\_SkyCMS_Controller
{
    
    private $_tabela = 'strony';
    
    /**
     * @route=(/)
     */
    public function index()
    {
        $db = Registry::get("database");
        $sql = Registry::get('SQL');
        
        $rodzice = $db->get_where($this->_tabela,array('rodzic'=>0));

        $stmt = $sql->prepare('SELECT * FROM strony WHERE rodzic<>0');

        $stmt->execute();

        $dzieci = $stmt->fetchAll();
        
        
        return array('dzieci'=>$dzieci,'rodzice'=>$rodzice);
    }
    
    
    /**
     * @route=(/tresci/dodaj)
     */
    public function dodaj()
    {
        
        $db = Registry::get("database");
        
        $result = $db->insert($this->_tabela,array('jezyk'=>request::post('jezyk'),
                                                    'priorytet'=>request::post('priorytet'),
                                                    'nazwa'=>request::post('nazwa'),
                                                    'tresc'=>request::post('tresc'),
                                                    'seo' => request::post('SEO'),
                                                    'rodzic' => request::post('id_rodzic'),
                                                    'template' => request::post('template')
                ));
        
        if(request::post('SEO'))
        {
            $db->insert('strony_seo',array('id_strony' => $db->insert_id()));
        }
        
        header('Location: '.URL.'_SkyCMS/');
    }
    
    
    /**
     * @route=(/tresci/edytuj/{id})
     */
    public function edytuj($array)
    {
        $id = $array[0];
        
        try{
            
            $db = Registry::get("database");
            
            $news = $db->get_where($this->_tabela, array('id'=>$id));
            
            if($news == null)
            {
                throw new \Exception('Nie znaleziono rekordu');
            }
            
        }
        catch(\Exception $e)
        {
            echo $e->getMessage();
            die();
        }
        
        return array('news'=>$news[0]);
    }
    
    
    /**
     * @route=(/tresci/podglad/stronaid={id}
     */
    function podglad($array)
    {
        $id = $array[0];
        
        $db = Registry::get("database");

        $news = $db->get_where($this->_tabela, array('id'=>$id));
        
        $template = $news[0]['template'];
        
        $news[0]['tresc'] = request::post('tresc');
        
        $data = explode('/',$template);
        $class = $data[0];
        $method = $data[1];
        
        $view = new \libs\view();
        $view->renderTwig($class, $method,array('news'=>$news[0]),true);
        
        return array();
    }
    
    /**
     * @route=(/tresci/SEO/zapisz/{id})
     */
    public function seo_zapisz($array)
    {
        $id = $array[0];
        
        $db = Registry::get("database");
        
        $result = $db->update('strony_seo', array(  'title'=>request::post('title'),
                                                    'keywords'=>request::post('keywords'),
                                                    'opis'=>request::post('opis')
                                                    ),
                                                array(
                                                    'id_strony'=>$id
                                                    ));
        
        
        return array();
        
    }
    
    
    /**
     * @route=(/tresci/SEO/{id})
     */
    public function edycja_seo($array)
    {
        $id = $array[0];
        
        try{
            
            $db = Registry::get("database");
            $news = $db->get_where('strony_seo', array('id_strony'=>$id));
            
            if($news == null)
            {
                throw new \Exception('Nie znaleziono rekordu');
            }
            
        }
        catch(\Exception $e)
        {
            echo $e->getMessage();
            die();
        }
        
        return array('news'=>$news[0]);
    }
    
    /**
     * 
     * @route=(/tresci/zapisz/{id})
     */
    public function zapisz($array)
    {
        $id = $array[0];
        
        $db = Registry::get("database");
        
        $result = $db->update($this->_tabela, array( 'template' => request::post('template'), 
                                                    'data_modyfikacji'=>date('Y-m-d H:i:s'),
                                                    'nazwa'=>request::post('nazwa'),
                                                    'tresc'=> request::post('tresc')
                                                    ),
                                                array(
                                                    'id'=>$id
                                                    ));
        
        
        return array();
    }
    
    
    /**
     * @route=(/tresci/usun/{id})
     */
    public function usun($array)
    {
        $id = $array[0];
        
        $db = Registry::get("database");
        
        $result = $db->delete($this->_tabela,array('id'=>$id));
        $result = $db->delete('strony_seo',array('id_strony'=>$id));
        
        header('Location: '.URL.'_SkyCMS');
    }
}

?>
